<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.push('/search')" />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      show-action
      :value="querySearch || '搜索商品'"
      @click="$router.push('/search')"
    >
      <template #action>
        <van-icon class="tool" name="apps-o" @click="$router.push('/category')"/>
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item" :class="{choiceActive: sortField === 'SYNTHESIZE'}" @click="doSearch('SYNTHESIZE')">综合</div>
      <div class="sort-item" :class="{choiceActive: sortField === 'SALESCOUNT'}" @click="doSearch('SALESCOUNT')">销量</div>
      <div class="sort-item" :class="{choiceActive: sortField === 'PRICE'}" @click="doSearch('PRICE')">价格 </div>
    </div>

    <div class="goods-list">
      <GoodsItem v-for="item in searchList" :key="item.id" :item="item"></GoodsItem>
    </div>
  </div>
</template>

<script>
import { getSearchList } from '@/api/product'
import GoodsItem from '@/components/GoodsItem.vue'
export default {
  name: 'SearchIndex',
  components: {
    GoodsItem
  },
  data () {
    return {
      sortField: 'SYNTHESIZE',
      searchList: []
    }
  },
  methods: {
    doSearch (sortField) {
      if (this.sortField === sortField) {
        return
      }
      this.sortField = sortField
      this.getList()
    },
    async getList () {
      const res = await getSearchList(this.sortField, this.querySearch)
      this.searchList = res.data
      if (this.searchList.length === 0) {
        this.$toast('没有匹配到 换个关键词试试')
      }
    }
  },
  computed: {
    querySearch () {
      return this.$route.query.search
    }
  },
  async created () {
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
  .choiceActive {
    color: rgb(249, 151, 168);
  }
}

// 商品样式
.goods-list {
  background-color: #f6f6f6;
}
</style>
